<template>
  <div>
    <el-dialog
      title="评论内容"
      :visible.sync="dialogVisible"
      width="700px"
      @closed="resetForm('form')"
      :lock-scroll="false"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="60px">
          <el-form-item label="评分">
            <el-rate v-model="form.score" style="margin-top: 10px"></el-rate>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input
              type="textarea"
              v-model="form.content"
              :rows="6"
              placeholder="请输入评论内容"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { sysEvaluateAdd } from '@/api/home/sysEvaluate.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        pid: '0', //父级节点(评价类型不传)
        resId: '', //资源ID
        content: '',
        score: 5, //评分
        type: '1', //节点类型；1-评价，2-回复
        resType: '', //资源类型；1-课程，2-知识点，3-…
      },
      rules: {
        content: [
          { required: true, message: '请输入评论内容', trigger: 'blur' },
        ],
      },
    }
  },
  props: ['resType', 'resId'],
  watch: {
    resId(v) {
      this.form.resId = v
    },
  },
  mounted() {
    this.form.resType = this.resType
    this.form.resId = this.resId

    Bus.$on('showDialogReviewAdd', () => {
      this.dialogVisible = true
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogReviewAdd')
  },
  methods: {
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.add()
        }
      })
    },
    //新增
    add() {
      sysEvaluateAdd(this.form).then((res) => {
        this.dialogVisible = false
        Bus.$emit('refreshReviewList')
        Bus.$emit('showScore')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.form.score = 5
      this.form.content = ''
    },
  },
}
</script>